package com.example.music.db;

import static com.example.music.db.DatabaseHelper.COLUMN_PLAYLIST_ID;
import static com.example.music.db.DatabaseHelper.COLUMN_PLAYLIST_NAME;
import static com.example.music.db.DatabaseHelper.TABLE_PLAYLISTS;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class PlaylistDAO {
    private DatabaseHelper dbHelper;

    public PlaylistDAO(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // 创建新歌单
    public long createPlaylist(String name) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_PLAYLIST_NAME, name);
        values.put(DatabaseHelper.COLUMN_CREATE_TIME, System.currentTimeMillis());

        long id = db.insertWithOnConflict(
                TABLE_PLAYLISTS,
                null,
                values,
                SQLiteDatabase.CONFLICT_IGNORE);

        db.close();
        return id;
    }

    // 删除歌单
    public boolean deletePlaylist(long playlistId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int result = db.delete(TABLE_PLAYLISTS,
                COLUMN_PLAYLIST_ID + "=?",
                new String[]{String.valueOf(playlistId)});
        db.close();
        return result > 0; // 如果删除了至少一行，则返回 true
    }

    // 获取所有歌单
    public List<Playlist> getAllPlaylists() {
        List<Playlist> playlists = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.query(TABLE_PLAYLISTS,
                new String[]{
                        COLUMN_PLAYLIST_ID,
                        COLUMN_PLAYLIST_NAME,
                        DatabaseHelper.COLUMN_CREATE_TIME
                },
                null, null, null, null,
                DatabaseHelper.COLUMN_CREATE_TIME + " DESC");

        if (cursor != null) {
            while (cursor.moveToNext()) {
                Playlist playlist = new Playlist();
                playlist.setId(cursor.getLong(0));
                playlist.setName(cursor.getString(1));
                playlist.setCreateTime(cursor.getLong(2));
                playlists.add(playlist);
            }
            cursor.close();
        }
        db.close();
        return playlists;
    }

    // 添加歌曲到歌单
    public long addSongToPlaylist(long playlistId, String songPath) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_PLAYLIST_REF, playlistId);
        values.put(DatabaseHelper.COLUMN_SONG_REF, songPath);

        long result = db.insertWithOnConflict(
                DatabaseHelper.TABLE_PLAYLIST_SONGS,
                null,
                values,
                SQLiteDatabase.CONFLICT_IGNORE);

        db.close();
        return result;
    }

    // 从歌单中移除歌曲
    public int removeSongFromPlaylist(long playlistId, String songPath) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int result = db.delete(DatabaseHelper.TABLE_PLAYLIST_SONGS,
                DatabaseHelper.COLUMN_PLAYLIST_REF + "=? AND " +
                        DatabaseHelper.COLUMN_SONG_REF + "=?",
                new String[]{String.valueOf(playlistId), songPath});
        db.close();
        return result;
    }

    // 获取歌单中的所有歌曲路径
    public List<String> getSongsInPlaylist(long playlistId) {
        List<String> songs = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.query(DatabaseHelper.TABLE_PLAYLIST_SONGS,
                new String[]{DatabaseHelper.COLUMN_SONG_REF},
                DatabaseHelper.COLUMN_PLAYLIST_REF + "=?",
                new String[]{String.valueOf(playlistId)},
                null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                songs.add(cursor.getString(0));
            }
            cursor.close();
        }
        db.close();
        return songs;
    }

    public boolean renamePlaylist(long playlistId, String newName) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_PLAYLIST_NAME, newName);

        int result = db.update(TABLE_PLAYLISTS, values,
                COLUMN_PLAYLIST_ID + "=?",
                new String[]{String.valueOf(playlistId)});

        db.close();
        return result > 0;
    }

    public static class Playlist {
        private long id;
        private String name;
        private long createTime;

        // Getters and setters

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public long getCreateTime() {
            return createTime;
        }

        public void setCreateTime(long createTime) {
            this.createTime = createTime;
        }
    }
}